import {_decorator, Component} from 'cc';
import {logUtil} from "../../framework/util/logUtil";
import {Effect} from "../../framework/fight/IEffectContainer";
import {zhuoshao} from "../../framework/fight/effect/zhuoshao";
import {zengshang} from "../../framework/fight/effect/zengshang";
import {bingdong} from "../../framework/fight/effect/bingdong";
import {LevelCon} from "./LevelCon";
import { baojilv } from '../../framework/fight/effect/texiao/baojilv';
import { digong } from '../../framework/fight/effect/texiao/digong';
import { disu } from '../../framework/fight/effect/texiao/disu';
import {dixue} from "../../framework/fight/effect/texiao/dixue";
import {gong} from "../../framework/fight/effect/texiao/gong";
import {hpMax} from "../../framework/fight/effect/texiao/hpMax";
import {huixue} from "../../framework/fight/effect/texiao/huixue";
import { mianshang } from '../../framework/fight/effect/texiao/mianshang';
import { shoubao } from '../../framework/fight/effect/texiao/shoubao';
import { shoumiss } from '../../framework/fight/effect/texiao/shoumiss';
import { zhenshang } from '../../framework/fight/effect/texiao/zhenshang';

const {ccclass} = _decorator;

@ccclass('EffectCon')
export class EffectCon extends Component {
    private static _instance: EffectCon;
    static get instance() {
        if (this._instance == null) {
            this._instance = new EffectCon(LevelCon.instance.BuffJson);
        }
        return this._instance;
    }

    InitJson: []

    //已经拥有的
    skillList : Effect[]=[]

    //备选池_选项池数据_(纯数据|类)
    skillChoiceList : Effect[]=[]

    constructor(buffJson:any) {
        super();
        this.InitJson=buffJson
        this.skillChoiceList=buffJson
    }

    //日志控制器
    log = logUtil.instance
    logLevel = "EffectCon"

    matchingSkill(buffJsonData:any): Effect{
        let effect:Effect
        switch (buffJsonData.class) {
            case "zhuoshao":
                effect=new zhuoshao(buffJsonData)
                break;
            case "zengshang":
                effect=new zengshang(buffJsonData)
                break;
            case "bingdong":
                effect=new bingdong(buffJsonData)
                break;
            case "baojilv":
                effect=new baojilv(buffJsonData)
                break;
            case "digong":
                effect=new digong(buffJsonData)
                break;
            case "disu":
                effect=new disu(buffJsonData)
                break;
            case "dixue":
                effect=new dixue(buffJsonData)
                break;
            case "gong":
                effect=new gong(buffJsonData)
                break;
            case "hpMax":
                effect=new hpMax(buffJsonData)
                break;
            case "huixue":
                effect=new huixue(buffJsonData)
                break;
            case "mianshang":
                effect=new mianshang(buffJsonData)
                break;
            case "shoubao":
                effect=new shoubao(buffJsonData)
                break;
            case "shoumiss":
                effect=new shoumiss(buffJsonData)
                break;
            case "zhenshang":
                effect=new zhenshang(buffJsonData)
                break;
            default:
                effect=null
                break;
        }
        if (effect) {
            this.addSkillList(effect);
        }
        return effect;
    }

    addSkillList(skill: Effect) {
        let existingItem = this.skillList.find(item => item.id == skill.id);
        //有则升级,无则新增
        if (existingItem) {
            existingItem.level++;
            skill=existingItem
        } else {
            this.skillList.push(skill);
        }

        if (skill.level == skill.levelMax) {
            this.skillChoiceList = this.skillChoiceList.filter(item => item.id != skill.id);
        }
    }

    getRandomObjs( count: number) :any[]{
        let objs=this.skillChoiceList
        if(objs.length==0){
            this.log.log(this.logLevel, `没有可供选择的对象`);
            return [];
        }
        if (objs.length < count) {
            this.log.log(this.logLevel, `可供选择的对象数不足`);
            return objs;
        }

        let availableObjs = objs.concat();//选项池
        let randomObjs = [];//随机项

        while (randomObjs.length < count&&availableObjs.length!=0) {
            // 从可用的对象中随机选择一个
            let randomIndex = Math.floor(Math.random() * availableObjs.length);
            let randomObj = availableObjs[randomIndex];
            // 添加到结果和最近选择中
            randomObjs.push(randomObj);
            // 从可用状态中移除已选择的状态
            availableObjs.splice(randomIndex, 1);
        }

        return randomObjs;
    }

}


